package cn.itacst.myServlet;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.connector.Request;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;


@WebServlet("/my")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet类中doGet方法执行了");

        String queryString = req.getQueryString();
        System.out.println("queryString:"+queryString);


        try {
            Field requestField = req.getClass().getDeclaredField("request");
            requestField.setAccessible(true);
            Request internalRequest = (Request) requestField.get(req);

            // 获取 Connector
            Connector connector = internalRequest.getConnector();
            String uriEncoding = connector.getURIEncoding();
            System.out.println("uriEncoding:"+uriEncoding);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

//        req.setCharacterEncoding("iso-8859-1");

        String addr = req.getParameter("addr");
        System.out.println("addr:"+addr);

        String username = req.getParameter("username");
        System.out.println("username直接打印:" + username);


        String newName = new String(username.getBytes("iso-8859-1"), "utf-8");
        System.out.println("username解密打印:" + newName);


        resp.setContentType("text/html;charset=utf-8");
        String prefix = "<font size='4' color='red'>";
        String postfix = "</font>";
        String content = "我来自MyServlet的doGet方法";
        resp.getWriter().print(prefix + content + postfix);

        System.out.println();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet类中doPost方法执行了");

//        req.setCharacterEncoding("utf-8");
//        req.setCharacterEncoding("gbk");

        String queryString = req.getQueryString();
        System.out.println("queryString:"+queryString);

        String fileEncoding = System.getProperty("file.encoding");
        System.out.println("fileEncoding:"+fileEncoding);

        req.setCharacterEncoding("utf-8");

        System.out.println(req.getContentType());


        String username = req.getParameter("username");
        System.out.println("username直接打印:" + username);

        String username2 = req.getParameter("username");
        System.out.println("username2直接打印:" + username2);


        resp.setContentType("text/html;charset=utf-8");
        String prefix = "<font size='4' color='red'>";
        String postfix = "</font>";
        String content = "我来自MyServlet的doPost方法";

        resp.getWriter().print(prefix + content + postfix);

        System.out.println();
    }
}
